import { coupon } from '../../../../js/modules/market';
import { api } from '../../../../js/util';
import { router } from '../../../../js/router';
var app = getApp();
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"myCouponList",

  /**
   * 页面的初始数据
   */
  data: {
    tablist: ['未使用（0）', '已使用（0）', '已过期（0）'], 
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    PageIndex: 1,
    PageSize: 20,
    used: 0,
    hasMoreData: true,
    loaded: false,
    couponList: [],
    isIphoneX:app.globalData.isIphoneX,//适配手机
    Currency:'',//货币符号
    mainColor: "#e56b14",
    receiveCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 20,
        });
      }
    });
    this.setData({
      mainColor: api.titleColor || "#e56b14"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Currency:api.Currency || '￥',
      loaded: false
    })
    this.loadCouponsCount();
    this.loadReceviedCoupons(this.data.used);
  },
  /**加载优惠券列表 */
  loadReceviedCoupons(used) {
    var that=this;
    var param = {
      PageIndex: that.data.PageIndex,
      PageSize: that.data.PageSize,
      Used: that.data.used
    };
    coupon.loadReceviedCoupons(param, function (res) {
      //console.log(res); 
      var contentlistTem = that.data.couponList;
      if (that.data.PageIndex == 1) {
        contentlistTem = [];
      }
      if (res.count > 0) {
        var contentlist = res.data[0].data;
        if (contentlist.length < that.data.PageSize) {
          that.setData({
            couponList: contentlistTem.concat(contentlist),
            hasMoreData: false,
            loaded: true
          });
        } else {
          that.setData({
            couponList: contentlistTem.concat(contentlist),
            hasMoreData: true,
            PageIndex: that.data.PageIndex + 1,
            loaded: true
          });
        }

      } else {
        that.setData({
          couponList: [],
          hasMoreData: true,
          PageIndex: 1,
          loaded: true
        });
      }
    });
  },
  /**获取优惠劵种类的数量 */
  loadCouponsCount(){
    var that = this;
    coupon.loadCouponsCount(function(res){
      var tablist = ['未使用（' + res.UnUsed + '）', '已使用（' + res.Used + '）', '已过期（' + res.OverUsed +'）'];
      that.setData({ tablist: tablist});
    });
    coupon.loadOnlineCouponsList({
      PageIndex: 1,
      PageSize: 20
    }, function (res) {
      that.setData({
        receiveCount: res.count || 0
      })
    })
  },
  onScanInstructEvent(e){
    var index = e.currentTarget.dataset.index;
    var item = this.data.couponList[index];
    var that = this;
    console.log('dddd--',item)
    if (item.instruction) {
      that.setData({
        showShadow:true,
        instruction: item.instruction,
        describes: item.describes
      });
    }
    else {
      coupon.getCouponInstructions({ ruleId: item.Id, isApply: item.IsApply,Currency:api.Currency || '￥' }, function (content) {
        console.log('content-----',content)
        that.setData({
          showShadow:true,
          instruction: content.instruction,
          describes: content.describes
        });
      });
    }
  },
  /**
 * 关闭弹层
 */
  shutShadow: function () {
    this.setData({
      showShadow: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadCouponsCount();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) { 
    if (this.data.hasMoreData) {
      this.loadReceviedCoupons();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * tabs切换
   */
  changeCurrent: function (res) {
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      used: res.detail.currentNum.id,
      PageIndex:1,
      loaded: false
    });
    this.loadReceviedCoupons(this.data.used);
  },
  /**
   * 领取优惠券
   */
  getMore: function () {
    router.navigate({
      path: 'receivedCouponList',
    })
  },
  onUseEvent:function(e){
    var index = e.currentTarget.dataset.index;
    var item = this.data.couponList[index];
    if (this.data.used!=0) return;
    app.globalData.product_list.rule_id=item.Id;
    router.navigate({
      path: "productList"
    });
  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  }

})